import 'package:flutter/material.dart';
import 'package:ribo_base_core/base_models/base_chart_formatter.dart';
import 'package:ribo_base_core/base_models/base_chart_model.dart';
import 'package:ribo_base_core/base_models/base_chart_pie_model.dart';
import 'base_chart.dart';

class BaseChartPie extends StatefulWidget {
  BaseChartPie({Key key, @required this.model}) : super(key: key);
  final BaseChartPieModel model;

  @override
  _BaseChartPieState createState() => _BaseChartPieState();
}

class _BaseChartPieState extends State<BaseChartPie> {
  Map<String, dynamic> _option;

  @override
  Widget build(BuildContext context) {
    _handleModel();

    return BaseChart(
      model: BaseChartModel(style: widget.model?.style, option: this._option),
    );
  }

  void _handleModel(){
    this._option = {
      "tooltip": {
        "show": false,
        "triggerOn": 'none',
        "trigger": 'item',
        "formatter": widget.model?.tooltipFormatter?.toJson(),
      },
      "aria": {
        "enabled": true,
        "decal": {
            "show": true
        }
      },
      "series": [
        {
          "type": 'pie',
          "radius": widget.model?.radius ?? '70%',
          'selectedMode': 'single',
          'minAngle': 16,
          "center": ['50%', '45%'],
          "itemStyle": {
            "decal": {
              "symbol": "none"
            }
          },
          "data": widget.model?.data,
          'label': {
            "show": true,
            "position": widget.model?.labelPosition,
            "fontSize": 10,
            'overflow': 'break',
            "alignTo": 'labelLine',
            'formatter': widget.model?.labelFormatter?.toJson(),
          },
          'labelLine': {
            'length': 5
          },
          "emphasis": {
            'scale': false
          },
          "animationType": 'scale',
          "animationEasing": 'elasticOut',
        }
      ]
    };
    if (widget.model?.isRose == true) {
      this._option['series'][0]['roseType'] = 'area';
      this._option['series'][0]['radius'] = [10, 80];
      this._option['series'][0]['label']['position'] = 'outside';
      this._option['series'][0]
      ['labelLine'] = {'show': true, 'length': 10, 'length2': 5};
    }
  }
}
